﻿using Catel.Articles._04___Unit_testing.UI.ViewModels;
using Catel.MVVM;
using Catel.MVVM.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Catel.Articles._04___Unit_testing.Test.UI.ViewModels
{
    [TestClass]
    public class MainWindowViewModelTest
    {
        [TestMethod]
        public void Initialization()
        {
            var mainWindowViewModel = new MainWindowViewModel();

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);
            Assert.AreEqual("Geert van Horrik", mainWindowViewModel.PersonCollection[0].FullName);
        }

        [TestMethod]
        public void AddPerson_Confirmed()
        {
            var mainWindowViewModel = new MainWindowViewModel();

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);
            Assert.IsTrue(mainWindowViewModel.Add.CanExecute(null));

            var uiVisualizerService = (MVVM.Services.Test.UIVisualizerService)mainWindowViewModel.GetService<IUIVisualizerService>();
            uiVisualizerService.ExpectedShowDialogResults.Enqueue(() => true);
            mainWindowViewModel.Add.Execute(null);

            Assert.AreEqual(2, mainWindowViewModel.PersonCollection.Count);
        }

        [TestMethod]
        public void AddPerson_Canceled()
        {
            var mainWindowViewModel = new MainWindowViewModel();

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);
            Assert.IsTrue(mainWindowViewModel.Add.CanExecute(null));

            var uiVisualizerService = (MVVM.Services.Test.UIVisualizerService)mainWindowViewModel.GetService<IUIVisualizerService>();
            uiVisualizerService.ExpectedShowDialogResults.Enqueue(() => false);
            mainWindowViewModel.Add.Execute(null);

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);
        }

        [TestMethod]
        public void EditPerson_Confirmed()
        {
            var mainWindowViewModel = new MainWindowViewModel();

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);
            Assert.IsFalse(mainWindowViewModel.Edit.CanExecute(null));
            mainWindowViewModel.SelectedPerson = mainWindowViewModel.PersonCollection[0];
            Assert.IsTrue(mainWindowViewModel.Edit.CanExecute(null));

            var uiVisualizerService = (MVVM.Services.Test.UIVisualizerService)mainWindowViewModel.GetService<IUIVisualizerService>();
            uiVisualizerService.ExpectedShowDialogResults.Enqueue(() =>
            {
                mainWindowViewModel.PersonCollection[0].FirstName = "New name";
                return true;
            });
            mainWindowViewModel.Edit.Execute(null);

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);
            Assert.AreEqual("New name", mainWindowViewModel.PersonCollection[0].FirstName);
        }

        [TestMethod]
        public void EditPerson_Canceled()
        {
            var mainWindowViewModel = new MainWindowViewModel();

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);
            Assert.IsFalse(mainWindowViewModel.Edit.CanExecute(null));
            mainWindowViewModel.SelectedPerson = mainWindowViewModel.PersonCollection[0];
            Assert.IsTrue(mainWindowViewModel.Edit.CanExecute(null));

            var uiVisualizerService = (MVVM.Services.Test.UIVisualizerService)mainWindowViewModel.GetService<IUIVisualizerService>();
            uiVisualizerService.ExpectedShowDialogResults.Enqueue(() => false);
            mainWindowViewModel.Edit.Execute(null);

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);
        }

        [TestMethod]
        public void RemovePerson_ConfirmWithYes()
        {
            var mainWindowViewModel = new MainWindowViewModel();

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);

            Assert.IsFalse(mainWindowViewModel.Remove.CanExecute(null));
            mainWindowViewModel.SelectedPerson = mainWindowViewModel.PersonCollection[0];
            Assert.IsTrue(mainWindowViewModel.Remove.CanExecute(null));

            var messageService = (MVVM.Services.Test.MessageService)mainWindowViewModel.GetService<IMessageService>();
            messageService.ExpectedResults.Enqueue(MessageResult.Yes);
            mainWindowViewModel.Remove.Execute(null);

            Assert.AreEqual(0, mainWindowViewModel.PersonCollection.Count);
        }

        [TestMethod]
        public void RemovePerson_ConfirmWithNo()
        {
            var mainWindowViewModel = new MainWindowViewModel();

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);

            Assert.IsFalse(mainWindowViewModel.Remove.CanExecute(null));
            mainWindowViewModel.SelectedPerson = mainWindowViewModel.PersonCollection[0];
            Assert.IsTrue(mainWindowViewModel.Remove.CanExecute(null));

            var messageService = (MVVM.Services.Test.MessageService)mainWindowViewModel.GetService<IMessageService>();
            messageService.ExpectedResults.Enqueue(MessageResult.No);
            mainWindowViewModel.Remove.Execute(null);

            Assert.AreEqual(1, mainWindowViewModel.PersonCollection.Count);
        }
    }
}
